package cn.lhjl.day09.多线程;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class DeathLock {
    public static void main(String[] args) {
        ReentrantLock lock1 = new ReentrantLock();
        ReentrantLock lock2 = new ReentrantLock();
        new Thread(){
            @Override
            public void run(){
                lock1.lock();
                System.out.println(Thread.currentThread().getName()+"获取到A锁");
                try {
                    lock2.tryLock(5000,TimeUnit.MILLISECONDS);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"获取到B锁");


            }
        }.start();
        new Thread(){
            @Override
            public void run(){
                lock2.lock();
                    System.out.println(Thread.currentThread().getName()+"获取到B锁");
                try {
                    lock1.tryLock(5000,TimeUnit.MILLISECONDS);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"获取到A锁");


            }
        }.start();
    }
}
